<?php

namespace common\modules\wechat\models\backend;

use common\helpers\Html;
use common\widgets\grid\ActionColumn;
use common\widgets\grid\FormulaColumn;
use common\widgets\GridView;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

class AccountSearch extends Account
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'type', 'status'], 'integer'],
            [['name', 'token', 'access_token', 'account', 'original', 'key', 'secret', 'encoding_aes_key', 'avatar', 'qrcode', 'address', 'description', 'username', 'password'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/wechat', 'Account List');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = self::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'type' => $this->type,
            'status' => $this->status,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'token', $this->token])
            ->andFilterWhere(['like', 'access_token', $this->access_token])
            ->andFilterWhere(['like', 'account', $this->account])
            ->andFilterWhere(['like', 'original', $this->original])
            ->andFilterWhere(['like', 'key', $this->key])
            ->andFilterWhere(['like', 'secret', $this->secret])
            ->andFilterWhere(['like', 'encoding_aes_key', $this->encoding_aes_key])
            ->andFilterWhere(['like', 'avatar', $this->avatar])
            ->andFilterWhere(['like', 'qrcode', $this->qrcode])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'password', $this->password]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [
            [
                'attribute' => 'id',
                'filterOptions' => [
                    'style' => 'width:80px',
                ],
            ],

            'name',
            'account',

            [
                'attribute' => 'type',
                'value' => function($model) {
                    return Html::tag('span', self::$types[$model->type], [
                        'class' => 'label label-info'
                    ]);
                },
                'format' => 'html',
                'width' => '150px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => self::$types,
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择类型'],
            ],

            [
                'attribute' => 'status',
                'value' => function($model) {
                    return Html::tag('span', self::$statuses[$model->status], [
                        'class' => 'label label-' . ($model->status == self::STATUS_ACTIVE ? 'success' : 'danger')
                    ]);
                },
                'format' => 'html',
                'width' => '150px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => self::$statuses,
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择状态'],
            ],

            [
                'class' => FormulaColumn::className(),
                'attribute' => 'created_at',
                'format'    => ['date', 'php:Y-m-d H:i:s'],
                'width' => '180px',
                'hAlign' => GridView::ALIGN_CENTER,
                'mergeHeader' => true,
            ],

            [
                'class' => ActionColumn::className(),
                'template' => '{manage} {update} {delete}',
                'width' => '180px',
                'buttons' => [
                    'manage' => function ($url, $model) {
                        return Html::a('管理此公众号', $url, [
                            'class' => 'text-danger',
                            'data-pjax' => 0
                        ]);
                    }
                ],
            ],
        ];
    }
}
